﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using DuJianliang.ApplicationPlatform.Common;
using DuJianliang.ApplicationPlatform.UserPermission;

namespace DuJianliang.ApplicationPlatform.Proj5715.Process
{
    public partial class ExchangeAuditSelect : System.Web.UI.Page
    {
        private string DispatchMissileProcessID
        {
            get
            {
                return ViewState["DispatchMissileProcessID"] == null ? string.Empty : ViewState["DispatchMissileProcessID"].ToString();
            }
            set
            {
                ViewState["DispatchMissileProcessID"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.DispatchMissileProcessID = Request.QueryString["DispatchMissileProcessID"];
                BindGridView();
            }
        }

        private void BindGridView()
        {
            if (string.IsNullOrWhiteSpace(this.DispatchMissileProcessID))
            {
                return;
            }

//            string sql = @"SELECT A.* FROM dbo.t5715_ExchangeAudit A
//                              INNER JOIN dbo.FlowInstance B ON A.FlowInstanceID = B.FlowInstanceID
//                              WHERE B.Completed = 1 AND OperationID = 2";

            string sql = @"SELECT T1.* FROM
                            (
	                            SELECT A.*
	                            FROM dbo.t5715_ExchangeAudit A
	                            INNER JOIN dbo.FlowInstance B ON A.FlowInstanceID = B.FlowInstanceID  
	                            WHERE B.Completed = 1 AND OperationID = 2  
                            ) T1 
                            INNER JOIN
                            (
	                            SELECT A.DispatchCode,B.MissileCode,C.processsortnumber ProcessID,C.DispatchMissileProcessID
	                            FROM dbo.t5715_Dispatch A
	                            INNER JOIN dbo.t5715_DispatchMissile B ON A.FlowInstanceID = B.DispatchID
	                            INNER JOIN dbo.t5715_DispatchMissileProcess C ON B.DispatchMissileID = C.DispatchMissileID
                            ) T2 ON T1.TargeDispatchCode = T2.DispatchCode 
		                            AND T1.TargeMissileCode = T2.MissileCode
		                            AND T1.TargeProcess = T2.ProcessID
                            WHERE T2.DispatchMissileProcessID = @DispatchMissileProcessID";

            Database database = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = database.GetSqlStringCommand(sql);
            database.AddInParameter(dbCommand, "@DispatchMissileProcessID", DbType.String, this.DispatchMissileProcessID);
            DataTable dataTable = database.ExecuteDataSet(dbCommand).Tables[0];

            gridView.DataSource = dataTable;
            gridView.DataBind();
        }

        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridView.PageIndex = e.NewPageIndex;
            BindGridView();
        }

        protected void gridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(e.CommandName) || e.CommandName.ToLower().Equals("page"))
            {
                return;
            }

            GridViewRow gridViewRow = ((GridViewRow)(((Button)(e.CommandSource)).Parent.Parent));
            string flowInstanceID = gridView.DataKeys[gridViewRow.RowIndex]["FlowInstanceID"].ToString();

            switch (e.CommandName)
            {
                case "CmdSelect":
                    InsertProcess(this.DispatchMissileProcessID, flowInstanceID);
                    Response.Redirect("ProcessExchangePart.aspx?DispatchMissileProcessID=" + this.DispatchMissileProcessID);
                    break;
                default:
                    break;
            }
        }

        private void InsertProcess(string processID, string flowInstanceID)
        {
            string sql = @"INSERT INTO dbo.t5715_ExchangePart
                            SELECT 
	                            NEWID() AS ExchangePartID
	                            ,'" + processID + @"' AS DispatchMissileProcessRouteID
	                            ,SourceDispatchCode AS Source_DispatchCode
	                            ,SourceMissileCode AS Source_MissileCode
	                            ,SourceProcess AS Source_WorkTeam
	                            ,SourcePartCode AS Source_PartCode
	                            ,SourcePartNumber AS Source_PartNumber
	                            ,TargeDispatchCode AS Targe_DispatchCode
	                            ,TargeMissileCode AS Targe_MissileCode
	                            ,TargeProcess AS Targe_WorkTeam
	                            ,TargePartCode AS Targe_PartCode
	                            ,TargePartNumber  AS Targe_PartNumber
	                            ,GETDATE() AS ExchangeDate
                            FROM dbo.t5715_ExchangeAudit WHERE FlowInstanceID = @FlowInstanceID";

            Database database = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = database.GetSqlStringCommand(sql);
            database.AddInParameter(dbCommand, "@FlowInstanceID", DbType.String, flowInstanceID);
            database.ExecuteNonQuery(dbCommand);
        }
    }
}